package com.owen.system.keep.history.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.owen.common.utils.ShiroUtils;
import com.owen.common.utils.SnowflakeIdWorker;
import com.owen.system.keep.history.dao.KeepConsumptionHistoryDao;
import com.owen.system.keep.history.dto.KeepConsumptionHistoryDTO;
import com.owen.system.keep.history.entity.KeepConsumptionHistory;
import com.owen.system.keep.history.service.KeepConsumptionHistoryService;
import com.owen.system.keep.history.vo.KeepConsumptionHistoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 收支记录
 */
@Service
public class KeepConsumptionHistoryServiceImpl implements KeepConsumptionHistoryService {

    @Autowired
    private KeepConsumptionHistoryDao keepConsumptionHistoryDao;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @Override
    public KeepConsumptionHistoryVO get(String id) {
        return keepConsumptionHistoryDao.get(id);
    }

    @Override
    public List<KeepConsumptionHistoryVO> list(Map<String, Object> map) {
        String userId = ShiroUtils.getUserId();
        Assert.isTrue(ObjectUtil.isNotEmpty(userId), "请登录");
        map.put("createBy", userId);
        return keepConsumptionHistoryDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        String userId = ShiroUtils.getUserId();
        Assert.isTrue(ObjectUtil.isNotEmpty(userId), "请登录");
        map.put("createBy", userId);
        return keepConsumptionHistoryDao.count(map);
    }

    @Override
    public int save(KeepConsumptionHistoryDTO keepConsumptionHistoryDTO) {
        String userId = ShiroUtils.getUserId();
        Assert.isTrue(ObjectUtil.isNotEmpty(userId), "请登录");
        KeepConsumptionHistory bean = BeanUtil.toBean(keepConsumptionHistoryDTO, KeepConsumptionHistory.class);
        bean.setId(snowflakeIdWorker.nextIdStr());
        bean.setCreateBy(userId);
        bean.setCreateTime(new Date());
        return keepConsumptionHistoryDao.save(bean);
    }

    @Override
    public int update(KeepConsumptionHistoryDTO keepConsumptionHistoryDTO) {
        String userId = ShiroUtils.getUserId();
        Assert.isTrue(ObjectUtil.isNotEmpty(userId), "请登录");
        KeepConsumptionHistory bean = BeanUtil.toBean(keepConsumptionHistoryDTO, KeepConsumptionHistory.class);
        bean.setCreateBy(userId);
        return keepConsumptionHistoryDao.update(bean);
    }

    @Override
    public int remove(String id) {
        return keepConsumptionHistoryDao.remove(id);
    }

    @Override
    public int batchRemove(String[] ids) {
        return keepConsumptionHistoryDao.batchRemove(ids);
    }
}
